package com.ky.home.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.param.TopicsAdd;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.home.entity.Checkins;
import com.ky.home.entity.Topics;
import com.ky.home.mapper.CheckinsMapper;
import com.ky.home.mapper.TopicsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/home/topics")
public class TopicsController {
    @Autowired
    private TopicsMapper topicsMapper;
    @Autowired
    private CheckinsMapper checkinsMapper;


    @RequestMapping("/clickIn")
    public R clickIn(HttpServletRequest request, @RequestBody TopicsAdd topicsAdd) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        String username= String.valueOf(JwtUtil.parseToken(token, "username"));
        Topics topics1 = new Topics();
        topics1.setUserId(id);
        QueryWrapper<Topics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", topics1.getUserId());
        Topics topics2 = topicsMapper.selectOne(queryWrapper);


        if (topics2 != null) {
            topics2.setDescription(topicsAdd.getDescription());
            topics2.setAddTime(new Date());
            topics2.setTitle(topicsAdd.getTitle());
            topics2.setNums(topics2.getNums() + 1);
            int result = topicsMapper.updateById(topics2);
            if (result <= 0) {
                return R.fail("打卡失败");
            }
            //    打卡成功 记录日志
            Checkins checkins = new Checkins();
            checkins.setUserId(topics2.getUserId())
                    .setTittle(topicsAdd.getTitle())
                    .setNums(topics2.getNums())
                    .setStatus(0)
                    .setContent(topicsAdd.getDescription())
                    .setAddAt(new Date());
            return R.ok(checkinsMapper.insert(checkins));

        } else {
            Topics topics = new Topics();
            topics.setUserId(id);
            topics.setAddTime(new Date());
            topics.setTitle(topicsAdd.getTitle());
            topics.setDescription(topicsAdd.getDescription());
            if (topics.getNums() == null) {
                topics.setNums(1);
            } else {
                topics.setNums(topics.getNums() + 1);
            }
            int insert2 = topicsMapper.insert(topics);
            if (insert2 <= 0) {
                return R.fail("打卡失败");
            }
            //    打卡成功 记录日志
            else {
                Checkins checkins = new Checkins();
                checkins.setUserId(topics.getUserId())
                        .setTittle(topicsAdd.getTitle())
                        .setNums(topics.getNums())
                        .setStatus(0)
                        .setContent(topicsAdd.getDescription())
                        .setAddAt(new Date());
                return R.ok(checkinsMapper.insert(checkins));
            }
        }
    }

    @RequestMapping("/getTopics")
    public R getTopics(HttpServletRequest request) {
        // String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        // Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        // QueryWrapper<Topics> queryWrapper = new QueryWrapper<>();
        // queryWrapper.eq("user_id", id);
        // Topics topics = topicsMapper.selectOne(queryWrapper);
        String header = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(header, "id")).intValue();
        QueryWrapper<Topics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        Topics topics = topicsMapper.selectOne(queryWrapper);
        if (topics == null) {
            return R.fail("您还没有打卡");
        }
        return R.ok(topics);
    }
}

